
layui.define(['table', 'form'], function(exports){
    var $ = layui.$
        ,admin = layui.admin
        ,view = layui.view
        ,table = layui.table
        ,form = layui.form;


    table.render({
        elem: '#LAY-db-link'
        ,url: 'dataSource'
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'id', width: 100, title: 'ID', sort: true}
            ,{field: 'hostname', title: '主机名', minWidth: 100}
            ,{field: 'port', title: '端口'}
            ,{field: 'username', title: '账号'}
            ,{field: 'password', title: '密码'}
            ,{field: 'connectionStatus', title: '状态',  templet: '#connectionStatus'}
            ,{title: '操作',  align:'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
        ]]
        ,page: true
        ,limit: 10
        // ,height: 'full-320'
        ,text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-db-link)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    admin.req({
                        type: 'delete',
                        url: 'dataSource',
                        traditional: true,
                        data:{"idList":data.id},
                        success: function (data) {
                            layer.msg(data.msg);
                            obj.del();
                            layer.close(index);
                        }
                    })

                });

        } else if(obj.event === 'edit'){
            admin.popup({
                title: '编辑用户'
                ,area: ['500px', '450px']
                ,id: 'LAY-popup-dblink-add'
                ,success: function(layero, index){
                    view(this.id).render('dbLink/dblinkform', data).done(function(){
                        form.render(null, 'layuiadmin-form-dblink');

                        //监听提交
                        form.on('submit(LAY-dblink-front-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            admin.req({
                                type: 'POST',
                                url: 'dataSource',
                                data: field,
                                success: function (data) {
                                    layer.msg(data.msg);
                                    layui.table.reload('LAY-db-link'); //重载表格
                                }
                            });
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        } else if (obj.event === 'look') {
            admin.popup({
                title: '系统当前参数'
                ,area: ['800px','1000px']
                , id: 'LAY-popup-dblink-look'
                , success: function (layero, index) {
                    view(this.id).render('dbLink/dblinksysdata', data).done(function(){
                        table.render({
                            elem: '#LAY-db-sys-data'
                            ,headers: { dsName: data.dataSource.pollName}
                            ,url: 'sys/sysData'
                            ,cols: [[
                                {field: 'name', width: '200px', title: 'name' }
                                ,{field: 'value',width: '400px', title: 'value' }
                            ]]
                           , width:800
                            ,page: false
                            ,text: '对不起，加载出现异常！'
                        });
                    })
                }
            });
        }
    });

    /* 监听指定开关 */
    form.on('switch(component-form-switchTest)', function(data){
        var connect = this.checked ? 'true' : 'false';
        var id = data.elem.attributes["data-id"].nodeValue;
        admin.req({
            type: 'POST',
            url: 'dataSource/'+id,
            data: {"connect":connect},
            success: function (data) {
                layer.msg(data.msg);
                layui.table.reload('LAY-db-link'); //重载表格
            }
        });
    });

    exports('syslink', {})
});